package com.es.util;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 描述:
 * es-servlet-
 *
 * @author Derek
 * 创建时间：2022-04-22 10:04
 */
public class DBUtils {
    private static final String CONFIG_FILE = "jdbc.properties";
    private static HikariDataSource dataSource = null;
    static{
        Properties properties = new Properties();
        InputStream input = DBUtils.class.getClassLoader().getResourceAsStream(CONFIG_FILE);
        try {
            properties.load(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(properties);
        //设置配置
        HikariConfig config = new HikariConfig(properties);
        //获取数据源

        dataSource = new HikariDataSource(config);
    }
    public static Connection getConnection(){
        if(dataSource == null){
            return null;
        }
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(DBUtils.getConnection());
    }
}
